import shutil
import baseTools
import os, sys
import sh

# path = './gitBinary/modulemanager'
# os.chdir(path)


def contain_binary(file,txt): 
 print(file)   
 try:
    data = open(file)
    lines = data.readlines()
    for line in lines:
        if 's.version          =' in line:
            result = line.split("'")[1]
            if txt in line:
             if 'Binary' in result:
                version = result.split("B")[0] 
                data.close()
                print('%s=====%s'%(result,version))
                return (True,True,version,result)
            data.close()
            # print(result)
            return (False,True,result,result)
    data.close()
    return (False,False,False,None)
 except IOError:
    print(IOError)
    # data.close()
    return (False,False,False,None)


def replace_version(file,old_str,new_str):
     """
      替换文件中的字符串
     :param file:文件名
     :param old_str:就字符串
     :param new_str:新字符串
     :return:
     """
     file_data = ""
     with open(file, "r", encoding="utf-8") as f:
         for line in f:
             if old_str in line:
                 print(line)
                 line = line.replace(old_str,new_str)
             file_data += line
     with open(file,"w",encoding="utf-8") as f:
         f.write(file_data)


# print(contain_binary('ModuleManager.podspec','Binary'))
# result = contain_binary('ModuleManager.podspec','Binary')
# version = result[2] + 'Binary'
#   s.version          = '0.1.27Binary'

# if result[1]:
#    print(version)




# print(result[1])
# if result[0]:
#  print()
# else:
#  print() 
#  alter('ModuleManager.podspec','0.1.27Binary','0.1.27')    

# alter('ModuleManager.podspec','0.1.27Binary','0.1.27')

# def alter(file,old_str,new_str):
#     """
#     将替换的字符串写到一个新的文件中，然后将原文件删除，新文件改为原来文件的名字
#     :param file: 文件路径
#     :param old_str: 需要替换的字符串
#     :param new_str: 替换的字符串
#     :return: None
#     """
#     with open(file, "r", encoding="utf-8") as f1,open("%s.bak" % file, "w", encoding="utf-8") as f2:
#         for line in f1:
#             if old_str in line:
#                 line = line.replace(old_str, new_str)
#             f2.write(line)
#     os.remove(file)
#     os.rename("%s.bak" % file, file)
    
    



# try:
#     data = open('ModuleManager.podspec')
#     lines = data.readlines()
#     for line in lines:
#         key = "s.version"
#         if key in line:
#            print(line)
#         #    result = line.replace("0.1.27Binary","0.1.27")
#            lines[11].replace("0.1.27Binary","0.1.27")
#         #    print(result)
#            print(data)

#            data.writelines(lines)
#            break
#     # print(data.read())
#     # print(data.readline(), end='')    # 读取一行内容
#     # print(data.readline(), end='')    # 读取二行内容
#     # data.seek(0)    # 返回文件起始位置
#     data.close()

# except IOError:
#     print('The data file is missing!')





# shutil.copy('./origin/ModuleManager.podspec','./')


# retval = os.getcwd()
# print ("当前工作目录为 %s" % retval)
# # os.system('chmod 777')

# cmd='chmod 777 ./build_pod.sh'
# cmd1='./build_pod.sh'

# # cmd='./build_pod.sh'
# data = os.popen(cmd)
# data = os.popen(cmd1)

# print(data.read())


# os.system('build_lib.sh')

# sh.RunningCommand("build_lib.sh")



# sh build_lib.sh


# shutil.copy('./git/modulemanager/LICENSE','./gitBinary/modulemanager')
# shutil.copy('./git/modulemanager/Pod','./gitBinary/modulemanager')
# file="build_lib.sh"
# old_path="./git/modulemanager/%s"%(file)
# to_path="./gitBinary/modulemanager"
# to_path_file="./gitBinary/modulemanager/%s"%(file)

# if not os.path.isdir(to_path):
#     os.makedirs(to_path)
# baseTools.copy('./git/modulemanager/Pod',to_path)

# try:
#    shutil.copy(old_path,to_path)
# except:
#     baseTools.createFile(to_path_file)
#     baseTools.copy(old_path,to_path_file)


# import shutil
# import os

# print ("os.path.realpath(__file__)=%s"%(os.path.realpath(__file__)))
# # shutil.copytree('./git/modulemanager/Pod','./gitBinary')
# to_path="./gitBinary"

# shutil.copytree('./git/modulemanager/build_lib.sh',to_path)


